﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Simple.Services.System.Recipes.Models;

namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 食材库管理
/// </summary>
[Route("api/FoodMaterials/[action]")]
[ApiController]
[Authorize]
public class FoodMaterialsController : ControllerBase
{
    private readonly FoodMaterialsService _foodMaterialsService;

    public FoodMaterialsController(FoodMaterialsService foodMaterialsService)
    {
        _foodMaterialsService = foodMaterialsService;
    }

    /// <summary>
    /// 食材列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> List([FromQuery] FoodMaterialsPageInputModel model)
    {
        List<FoodMaterialsModel> data = await _foodMaterialsService.GetAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 食材查询
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> Page([FromQuery] FoodMaterialsPageInputModel model)
    {
        PageResultModel<FoodMaterialsModel> data = await _foodMaterialsService.GetPageAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 食材增加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Add([FromBody] FoodMaterialsModel model)
    {
        await _foodMaterialsService.AddAsync(model);
        return AppResult.Status200OK("新增成功");
    }

    /// <summary>
    /// 食材编辑
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Edit([FromBody] FoodMaterialsModel model)
    {
        await _foodMaterialsService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功");
    }

    /// <summary>
    /// 食材删除
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
    {
        await _foodMaterialsService.DeleteAsync(models.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }

    /// <summary>
    /// 食材分类列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> TypeList()
    {
        var data = await _foodMaterialsService.GetTypeListAsync();
        return AppResult.Status200OK(data: data);
    }

}
